﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LDSP.BRL;
using LDSP.Util;
using System.Data;

namespace ImplementationInitialization
{
    public static class CodificationImport
    {
        static IDictionary<string, Catalog> _catalogDic = null;
        static IDictionary<string, Category> _categoryDic = null;

        public static void Run()
        {
            if (_catalogDic == null)
            {
                InitialData();
            }

            ImportFile(@"..\..\..\..\Doc\SampleData\法律法规及标准\上海-环境InfoList20110421T140225.xls", _catalogDic["环境"].ID);
            ImportFile(@"..\..\..\..\Doc\SampleData\法律法规及标准\世博专栏InfoList20110509T112039.xls", _catalogDic["综合管理"].ID);
            ImportFile(@"..\..\..\..\Doc\SampleData\法律法规及标准\固体废弃物污染_标准InfoList20110509T112056.xls", _catalogDic["固体废弃物"].ID, DocType.标准);
            ImportFile(@"..\..\..\..\Doc\SampleData\法律法规及标准\固体废弃物污染InfoList20110509T112052.xls", _catalogDic["固体废弃物"].ID);
            ImportFile(@"..\..\..\..\Doc\SampleData\法律法规及标准\土壤污染InfoList20110509T112036.xls", _catalogDic["土壤"].ID);
            ImportFile(@"..\..\..\..\Doc\SampleData\法律法规及标准\安全生产_标准InfoList20110509T114657.xls", _catalogDic["安全生产"].ID, DocType.标准);
            ImportFile(@"..\..\..\..\Doc\SampleData\法律法规及标准\清洁生产和能源管理_标准InfoList20110509T112106.xls", _catalogDic["清洁生产和能源管理"].ID, DocType.标准);
            ImportFile(@"..\..\..\..\Doc\SampleData\法律法规及标准\环境综合 InfoList20110531T103129.xls", _catalogDic["环境综合管理"].ID);
            ImportFile(@"..\..\..\..\Doc\SampleData\适用法律法规及标准\导出列表1.xls", _catalogDic["综合管理"].ID);
            ImportFile(@"..\..\..\..\Doc\SampleData\适用法律法规及标准\导出列表2.xls", _catalogDic["综合管理"].ID);
            ImportFile(@"..\..\..\..\Doc\SampleData\适用法律法规及标准\导出列表3.xls", _catalogDic["综合管理"].ID);
            ImportFile(@"..\..\..\..\Doc\SampleData\适用法律法规及标准\导出列表4.xls", _catalogDic["综合管理"].ID);
        }

        private static void ImportFile(string filename, int catalogID)
        {
            ImportFile(filename, catalogID, DocType.法律法规);
        }

        private static void ImportFile(string filename, int catalogID, DocType docType)
        {
            ExcelClass cls = new ExcelClass(filename);
            DataTable dt = cls.ToDataTable(0);

            Codification cod = null;
            int number = 0;
            foreach (DataRow row in dt.Rows)
            {
                //跳过最后一行，最后一行是Excel里的第二行。
                if (number == dt.Rows.Count - 1)
                {
                    continue;
                }
                number++;

                try
                {
                    cod = new Codification();

                    string name = string.Empty;
                    string referenceNumber = string.Empty;
                    if (docType == DocType.法律法规)
                    {
                        name = row[Constants.ColumnNames.COLUMN_TITLE] != null ? row[Constants.ColumnNames.COLUMN_TITLE].ToString() : string.Empty;
                        referenceNumber = row[Constants.ColumnNames.COLUMN_PUBLISHD_SYMBOL] != null ? row[Constants.ColumnNames.COLUMN_PUBLISHD_SYMBOL].ToString() : string.Empty;
                    }
                    else if (docType == DocType.标准)
                    {
                        name = row[Constants.ColumnNames.COLUMN_STANDARD_TITLE] != null ? row[Constants.ColumnNames.COLUMN_STANDARD_TITLE].ToString() : string.Empty;
                        referenceNumber = row[Constants.ColumnNames.COLUMN_STANCARD_PUBLISHD_SYMBOL] != null ? row[Constants.ColumnNames.COLUMN_STANCARD_PUBLISHD_SYMBOL].ToString() : string.Empty;
                    }

                    DateTime releaseDT = DateTime.MinValue;
                    if (row[Constants.ColumnNames.COLUMN_ISSUE_DATE] != null && DateTime.TryParse(row[Constants.ColumnNames.COLUMN_ISSUE_DATE].ToString(), out releaseDT))
                    {
                        releaseDT = DateTime.Parse(row[Constants.ColumnNames.COLUMN_ISSUE_DATE].ToString());
                    }

                    DateTime implementDT = DateTime.MinValue;
                    if (row[Constants.ColumnNames.COLUMN_EFFCTIVE_DATE] != null && DateTime.TryParse(row[Constants.ColumnNames.COLUMN_EFFCTIVE_DATE].ToString(), out implementDT))
                    {
                        implementDT = DateTime.Parse(row[Constants.ColumnNames.COLUMN_EFFCTIVE_DATE].ToString());
                    }

                    cod.CatalogID = catalogID;
                    cod.CategoryID = _categoryDic.ContainsKey(row[Constants.ColumnNames.COLUMN_CATEGORY].ToString()) ? _categoryDic[row[Constants.ColumnNames.COLUMN_CATEGORY].ToString()].ID : 0;
                    cod.Name = name;
                    cod.Organization = row[Constants.ColumnNames.COLUMN_PROMULGATING_AGENCY] != null ? row[Constants.ColumnNames.COLUMN_PROMULGATING_AGENCY].ToString() : string.Empty;
                    cod.ReferenceNumber = referenceNumber;
                    cod.DocType = row[Constants.ColumnNames.COLUMN_CATEGORY].ToString() == "标准" ? DocType.标准 : docType;//目录为“标准”，则文档类型应为标准，尽管可能是从法律法规模板里导入的。
                    if (releaseDT != DateTime.MinValue)
                    {
                        cod.ReleaseDT = releaseDT;
                    }
                    if (implementDT != DateTime.MinValue)
                    {
                        cod.ImplementDT = implementDT;
                    }
                    cod.UploadDT = DateTime.Now;

                    if (cod.CategoryID == 0)
                    {
                        //调整标准的栏目数据
                        if (docType == DocType.标准)
                        {
                            string code = row[Constants.ColumnNames.COLUMN_STANCARD_PUBLISHD_SYMBOL].ToString();
                            if (code.IndexOf("GB/T") >= 0)
                            {
                                cod.CategoryID = _categoryDic["国家强制性标准"].ID;
                            }
                            else if (code.IndexOf("GB") >= 0)
                            {
                                cod.CategoryID = _categoryDic["国家推荐性标准"].ID;
                            }
                            else if (code.IndexOf("/T") >= 0)
                            {
                                cod.CategoryID = _categoryDic["行业强制性标准"].ID;
                            }
                            else
                            {
                                cod.CategoryID = _categoryDic["行业推荐性标准"].ID;
                            }
                        }
                        else
                        {
                            string category = row[Constants.ColumnNames.COLUMN_CATEGORY].ToString();
                            if (category == "法律")
                            {
                                cod.CategoryID = _categoryDic["国家法律"].ID;
                            }
                            else if (category == "规章")
                            {
                                cod.CategoryID = _categoryDic["部门规章"].ID;
                            }
                        }
                    }

                    cod.Add();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error import : " + "File Name : " + filename + " Codification : " + cod.Name
                        + "\n" + "Message : " + ex.Message);
                }
            }
        }

        private static void InitialData()
        {
            Catalog cl = new Catalog();
            List<Catalog> catalogs = cl.GetAllCatalogs();

            _catalogDic = new Dictionary<string, Catalog>();
            foreach (Catalog l in catalogs)
            {
                if (!_catalogDic.ContainsKey(l.Name))
                {
                    _catalogDic.Add(l.Name, l);
                }
            }

            Category cr = new Category();
            List<Category> categories = cr.GetAllCategories();

            _categoryDic = new Dictionary<string, Category>();
            foreach (Category c in categories)
            {
                if (!_categoryDic.ContainsKey(c.Name))
                {
                    _categoryDic.Add(c.Name, c);
                }
            }
        }
    }
}
